require('./Timerange.styl');
const { Toast } = SaltUI;
const { DatetimeField } = SaltUI;
class Timerange extends React.Component {

    constructor(props) {
        super(props);
        let now1 = new Date().setMonth((new Date().getMonth()-1));
        let now = new Date().getTime();  
        this.state = {
            startTime: now1,
            endTime: now,
        };
    }
    handleChange(name, value) {
        //debugger
        if (name == 'startTime'&& value >this.state.endTime) {
            Toast.show({
                type: 'error',
                content: '起始时间过大'
            });
            return;
        } else if (name == 'endTime'&& value <this.state.startTime) {
            Toast.show({
                type: 'error',
                content: '结束时间过小'
            });
            return;
        } else {
            this.setState({
                [name + '1']: new Date(value).toLocaleDateString(),
                [name]: value
            }, function () {
                this.props.onchange(this.state.startTime, this.state.endTime);
            });
        }

    }

    render() {
        let t = this;
        return (
            <div className="timerange">
                <p className="timeRange">时间范围</p>
                <DatetimeField
                    label="开始"
                    columns={['年', '月', '日']}
                    className="time"
                    value={this.state.startTime}
                    onSelect={this.handleChange.bind(t, 'startTime')} />
                <DatetimeField
                    label="结束"
                    columns={['年', '月', '日']}
                    className="time"
                    value={this.state.endTime}
                    onSelect={this.handleChange.bind(t, 'endTime')} />
            </div>
        );
    }

    componentWillMount() {
    }

    componentDidMount() {
        this.props.onchange(this.state.startTime, this.state.endTime);
    }

    componentWillReceiveProps(nextProps) {
    }

    shouldComponentUpdate(nextProps, nextState) {
        return true;
    }

    componentWillUpdate(nextProps, nextState) {
    }

    componentDidUpdate(prevProps, prevState) {
    }

    componentWillUnmount() {
    }
}

module.exports = Timerange;
